<?php 
namespace App\Lib;

class Curl
{
	public static $instance = null;

	public static function getInstance()
	{
		if (self::$instance == null) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	public static function http_get($url,$lang = 'en-us')
	{
		$oCurl = curl_init();

		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		
		curl_setopt($oCurl, CURLOPT_TIMEOUT,6); 
		
		curl_setopt($oCurl, CURLOPT_ENCODING, 'gzip, deflate');
		
		curl_setopt($oCurl, CURLOPT_USERAGENT,  'Mozilla/5.0 (Windows NT 6.1; rv:40.0) Gecko/20100101 Firefox/40.0');
		
		if($lang == 'zh-cn'){
			curl_setopt($oCurl, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
		}

		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);

		curl_close($oCurl);
		if (intval($aStatus["http_code"]) == 200) {
			return $sContent;
		}
		return false;
	}

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    public static function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}